//
//  LLUserCenterVCViewController.m
//  LoveLove
//
//  Created by 吴志强 on 2019/7/4.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLUserCenterVCViewController.h"
#import "LLStatusModel.h"
#import "LLNewsVideoDetialVC.h"
#import "LLNewsImageDetialVC.h"

#import "videocell.h"
#import "oneImageCell.h"
#import "threeImageCell.h"
#import "titleTableViewCell.h"

@interface LLUserCenterVCViewController ()<UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIImageView *image;
@property (nonatomic,strong) UIButton *guanzhu_btn;
@property (nonatomic,strong) UILabel *tuwen_label;
@property (nonatomic,strong) UIView *line1;
@property (nonatomic,strong) UILabel *shiping_label;
@property (nonatomic,strong) UIView *line2;
@property (nonatomic,assign) int type;

@end

@implementation LLUserCenterVCViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;

    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView];

    [self init_view];

    self.view.backgroundColor = [UIColor whiteColor];

    self.type = 1;
    self.page = 1;

    [self get_detial];
}

- (void) init_view
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.navH, LLScreenWidth, LLScreenHeight)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];

    _image = [[UIImageView alloc] initWithFrame:CGRectMake(15, 50, 80, 80)];
    _image.backgroundColor = [UIColor redColor];
    _image.layer.cornerRadius = 40;
    _image.contentMode = UIViewContentModeScaleAspectFit;
    _image.layer.masksToBounds = YES;
    [view addSubview:_image];

    float width = LLScreenWidth - 150;

    float single_width = width/4.0;

    NSArray *array = @[@"关注",@"粉丝",@"发布",@"积分"];
    for (int i = 0; i<array.count; i++) {

        UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(110+i*single_width, _image.top, single_width, 50)];
        [view addSubview:backview];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, backview.width, backview.height*0.6)];
        label.font = [UIFont systemFontOfSize:19];
        label.tag = 1000+i;
        label.textAlignment = NSTextAlignmentCenter;
        [backview addSubview:label];

        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, label.bottom, backview.width, backview.height*0.4)];
        label2.font = [UIFont systemFontOfSize:13];
        label2.text = array[i];
        label2.textAlignment = NSTextAlignmentCenter;
        [backview addSubview:label2];
    }

    _guanzhu_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _guanzhu_btn.frame = CGRectMake(110, _image.bottom-20, width, 30);
    _guanzhu_btn.backgroundColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    _guanzhu_btn.layer.cornerRadius = 8;
    _guanzhu_btn.layer.masksToBounds = YES;
    [_guanzhu_btn addTarget:self action:@selector(guanzhu) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_guanzhu_btn];

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, _guanzhu_btn.bottom+20, LLScreenWidth-30, 1)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];

    UILabel *line_text = [[UILabel alloc] initWithFrame:CGRectMake(0, line.bottom+10, LLScreenWidth, 20)];
    line_text.text = @"ta的发布";
    line_text.textAlignment = NSTextAlignmentCenter;
    [view addSubview:line_text];

    _tuwen_label = [[UILabel alloc] initWithFrame:CGRectMake(LLScreenWidth/2-50, line_text.bottom+20, 50, 20)];
    _tuwen_label.textAlignment = NSTextAlignmentCenter;
    _tuwen_label.text = @"图文";
    _tuwen_label.userInteractionEnabled = YES;
    _tuwen_label.textColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    [_tuwen_label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)]];
    [view addSubview:_tuwen_label];

    _line1 = [[UIView alloc] initWithFrame:CGRectMake(LLScreenWidth/2-50, _tuwen_label.bottom+10, 50, 1)];
    _line1.backgroundColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    [view addSubview:_line1];

    _shiping_label = [[UILabel alloc] initWithFrame:CGRectMake(LLScreenWidth/2, line_text.bottom+20, 50, 20)];
    _shiping_label.textAlignment = NSTextAlignmentCenter;
    _shiping_label.text = @"视频";
    _shiping_label.userInteractionEnabled = YES;
    [_shiping_label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click2)]];
    [view addSubview:_shiping_label];

    _line2 = [[UIView alloc] initWithFrame:CGRectMake(LLScreenWidth/2, _shiping_label.bottom+10, 50, 1)];
    _line2.backgroundColor = [UIColor whiteColor];
    [view addSubview:_line2];

    view.height = _line2.bottom+10;

    self.tableView.top = view.bottom;
    self.tableView.height = LLScreenHeight-view.bottom;
}

- (void) get_detial
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"25" forKey:@"edu_id"];
    [dic setValue:self.user_id forKey:@"userid_other"];

    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {

        NSDictionary *dict = result[@"data"];

        NSArray *data = @[[NSString stringWithFormat:@"%@",dict[@"user_follow"]],
                          [NSString stringWithFormat:@"%@",dict[@"user_etc"]],
                          [NSString stringWithFormat:@"%@",dict[@"user_report"]],
                          [NSString stringWithFormat:@"%@",dict[@"user_point"]]];

        for (int i = 0; i<data.count; i++) {

            UILabel *label = [self.view viewWithTag:1000+i];
            label.text = data[i];
        }

        [self.image sd_setImageWithURL:[NSURL URLWithString:dict[@"user_avatar"]]];

        if ([dict[@"is_follow"] intValue] == 0) {
            [self.guanzhu_btn setTitle:@"互相不关注" forState:UIControlStateNormal];
        }
        else if ([dict[@"is_follow"] intValue] == 1) {
            [self.guanzhu_btn setTitle:@"你关注了他" forState:UIControlStateNormal];
        }
        else if ([dict[@"is_follow"] intValue] == 2) {
            [self.guanzhu_btn setTitle:@"他关注了你" forState:UIControlStateNormal];
        }
        else if ([dict[@"is_follow"] intValue] == 3) {
            [self.guanzhu_btn setTitle:@"互相关注" forState:UIControlStateNormal];
        }

        self.titleLabel.text = dict[@"user_name"];

        [self getlist];

    } failure:^(NSString *msg) {

        [SVProgressHUD showErrorWithStatus:msg];
    }];
}

- (void) getlist
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"26" forKey:@"edu_id"];
    [dic setValue:self.user_id forKey:@"userid_other"];
    [dic setValue:@(self.type) forKey:@"range"];
    [dic setValue:@(self.page) forKey:@"page"];

    if (self.page == 1) {
        [self.models removeAllObjects];
        [self.tableView reloadData];
    }

    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {

        NSArray *array = result[@"data"];

        for (NSInteger i = 0; i < array.count; i++) {

            NSDictionary *dict = array[i];
            LLStatusModel *model = LLStatusModel.new;

            if ([dict[@"type"] intValue] == 1 ||
                [dict[@"type"] intValue] == 2 ||
                [dict[@"type"] intValue] == 3 ||
                [dict[@"type"] intValue] == 4) {


                model.comment_num = [NSString stringWithFormat:@"%@",dict[@"comment_num"]];
                model.newsId = [NSString stringWithFormat:@"%@",dict[@"id"]];
                model.img_url = dict[@"img_url"] ? dict[@"img_url"] : @[];
                model.is_family = [NSString stringWithFormat:@"%@",dict[@"is_family"]];
                model.look_num = [NSString stringWithFormat:@"%@",dict[@"look_num"]];
                model.newsTitle = [NSString stringWithFormat:@"%@",dict[@"new_title"]];
                model.u_name = [NSString stringWithFormat:@"%@",dict[@"u_name"]];
                model.type = [NSString stringWithFormat:@"%@",dict[@"type"]];
                model.video_img = [NSString stringWithFormat:@"%@",dict[@"video_img"]];
                model.video_url = [NSString stringWithFormat:@"%@",dict[@"video_url"]];

                model.time = [self compareCurrentTime:[self getDateFromString:dict[@"time"]]];
            }

            [self.models addObject:model];
        }

        [self.tableView reloadData];

        [SVProgressHUD dismiss];

    } failure:^(NSString *msg) {

        [SVProgressHUD showErrorWithStatus:msg];
    }];
}

- (void) click1
{
    _tuwen_label.textColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    _line1.backgroundColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    _shiping_label.textColor = [UIColor blackColor];
    _line2.backgroundColor = [UIColor whiteColor];

    self.type = 2;
    self.page = 1;
    [self getlist];
}

- (void) click2
{
    _tuwen_label.textColor = [UIColor blackColor];
    _line1.backgroundColor = [UIColor whiteColor];
    _shiping_label.textColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];
    _line2.backgroundColor = [UIColor colorWithRed:227/255.0 green:15/255.0 blue:29/255.0 alpha:1];

    self.type = 1;
    self.page = 1;
    [self getlist];
}

- (void) guanzhu
{

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    LLStatusModel *model = self.models[indexPath.row];
    if ([model.type intValue] == 1) {

        return 290;
    }
    else if ([model.type intValue] == 2) {

        return 120;
    }
    else if ([model.type intValue] == 3) {

        return 190;
    }
    else if ([model.type intValue] == 4) {

        return 120;
    }
    else if ([model.type intValue] == 1000) {

        return 290;
    }

    return 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.models.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    LLStatusModel *model = self.models[indexPath.row];
    if ([model.type intValue] == 1) {

        static NSString *string = @"videocell";
        videocell *cell = [[videocell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 2) {

        static NSString *string = @"oneImageCell";
        oneImageCell *cell = [[oneImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 3) {

        static NSString *string = @"threeImageCell";
        threeImageCell *cell = [[threeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }
    else if ([model.type intValue] == 4) {

        static NSString *string = @"titleTableViewCell";
        titleTableViewCell *cell = [[titleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
        cell.model = model;
        return cell;
    }

    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLStatusModel *model = self.models[indexPath.row];

    if ([model.type intValue] == 1) {

        LLNewsVideoDetialVC *vc = [LLNewsVideoDetialVC new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.type intValue] == 2 ||
             [model.type intValue] == 3 ||
             [model.type intValue] == 4) {

        LLNewsImageDetialVC *vc = [LLNewsImageDetialVC new];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
